﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using SFP;
using SFP.Animation2D;
using SFP.Input;
using HamsterDelivery.Menus;
using HamsterDelivery.LevelStructure;

namespace HamsterDelivery
{
    public class HamsterGame : Game
    {
        public const int FADE_TIME = 750;
        
        public bool InIntro { get; set; }
        public bool InMenu { get; set; }
        public bool InGame { get; set; }
        
        public Form Form { get; private set; }
        public Fader Fader { get; private set; }
        public Intro Intro { get; private set; }
        public Level Level { get; private set; }
        public HUD HUD { get; set; }
        public Player Player { get; private set; }

        public int LetterScore { get; set; }
        public int PackageScore { get; set; }

        public HamsterGame(Size resolution) : base(resolution)
        {
            showFPS = false;
            limitFrameRate = false;
            maxFrameRate = 120;

            Form = GetForm();
            Form.StartPosition = FormStartPosition.CenterScreen;
        }

        protected override void Initialize()
        {
            InIntro = true;
            InMenu = false;
            InGame = false;

            Fader = new Fader(this, FADE_TIME);
            Intro = new Intro(this);
            Level = new Level(this);
            HUD = new HUD(this);
            Player = new Player(40, 400, this);

            MenuManager.initMenus(this);
        }

        protected override void LoadContent()
        {
            Resources.loadRessources();

            Player.setUpAnimations();
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(double time)
        {
            if (time > 50) time = 50;

            KeyboardState keyStates = Keyboard.GetKeyboardState();

            Fader.update(time);

            if (InIntro)
            {
                Intro.update(time);
                return;
            }

            if (InMenu)
                MenuManager.ActiveMenu.update(keyStates);
            else if (InGame)
            {
                Player.update(time, keyStates);
                Level.update(time);
            }

            processEscape(keyStates);
        }

        public void processEscape(KeyboardState keyStates)
        {
            if (keyStates.isKeyDownNotPressed(Keys.Escape))
            {
                if (InGame)
                {
                    MenuManager.ActiveMenu = MenuManager.PauseMenu;
                    InMenu = !InMenu;
                }
                else
                {
                    if (MenuManager.ActiveMenu != MenuManager.StartMenu)
                        MenuManager.ActiveMenu = MenuManager.StartMenu;
                    else
                        Exit();
                }
            }
        }

        protected override void Draw(Graphics graphics)
        {
            if (InGame)
            {
                /* Draw level and player... */
                Level.draw(graphics);

                /* Draw HUD */
                HUD.draw(graphics);
            }

            if (InMenu)
            {
                MenuManager.ActiveMenu.draw(graphics);
            }

            if (InIntro)
            {
                Intro.draw(graphics);
            }

            Fader.draw(graphics);
        }
    }
}
